<template>

	<view class="login-container">
		<!-- 添加返回按钮 -->
		<view class="back-button" @tap="goBack">
			<image src="/static/images/return.png" mode="aspectFit" class="back-icon"></image>
		</view>

		<view class="login-header">
			<!-- applogo -->
			<view class="login-logo-container">
				<image class="login-logo" src="/static/images/logoIcon.jpg" mode="aspectFit"></image>
			</view>
			<!-- <text class="login-welcome">Hello!</text> -->
			<text class="login-subtitle">欢迎来到趣趣玩</text>
		</view>

		<!-- tab表单切换 -->
		<view class="login-tabs">
			<!-- <view class="tab-item" :class="{ active: activeTab === 'quick' }" @click="switchTab('quick')">
				<text>手机登录</text>
				<view class="tab-line" v-if="activeTab === 'quick'"></view>
			</view> -->
			<view class="tab-item" :class="{ active: activeTab === 'password' }"
				@click="switchTab('password');switchButton('login')">
				<text>登录</text>
				<view class="tab-line" :style="{ transform: activeTab === 'register' ? 'translateX(130%)' : 'translateX(0)' }"></view>
			</view>
			<view class="tab-item" :class="{ active: activeTab === 'register' }"
				@click="switchTab('register');switchButton('register')">
				<text>注册</text>
			</view>
		</view>

		<view class="login-form">
			<!-- 密码登录表单 -->
			<view v-if="activeTab==='password'" class="login-form-box">
				<view class="login-password" v-if="loginway===1">
					<view class="inputf">
						<input type="text"  v-model="accountOrPhone" />
						<span class="label" :style="accountOrPhone ? {top:'-20px'}:{}">手机号</span>
					</view>
					<view class="inputf">
						<!-- <input type="password" placeholder="密码" /> -->
						<span class="label" :style="password ? {top:'-20px'}:{}">密码</span>
						<input :type="showPassword ? 'text' : 'password'" v-model="password"
							@input="checkPasswordFormat('password')" />
						<image v-if="password" class="password-eye"
							:src="showPassword ? '/static/images/eye-open.png' : '/static/images/eye-close.png'"
							@click="togglePasswordVisibility('password')" mode="aspectFit"></image>
					</view>
				</view>
				<view class="login-password" v-if="loginway===0">
					<view class="inputf">
						<input type="text" v-model="accountOrPhone" />
						<span class="label" :style="accountOrPhone ? {top:'-20px'}:{}">手机号</span>
					</view>
					<view class="inputf-box">
						<view class="inputf" style="width: 60%;">
							<input type="text" style="border-radius: 18px 0px 0px 18px;" v-model="logincode" />
							<span class="label" :style="logincode ? {top:'-20px'}:{}">验证码</span>

						</view>
						<view class="login-sendcode" @click="sendLoginCode" :class="{'disabled': loginCodeCountdown < 60}">
							{{ loginCodeCountdown < 60 ? `${loginCodeCountdown}秒后重发` : '发送验证码' }}
						</view>
					</view>

				</view>
				<view class="login-options">
					<text class="login-option" @click="changeloginway">{{logintext}}</text>
					<text class="login-option" @click="forgotPassword">忘记密码</text>
				</view>
			</view>


			<!-- 注册表单 -->
			<view v-else-if="activeTab==='register'" class="register-form">
				<view class="register-input">
					<input type="text" placeholder="手机号" v-model="registerPhone" />
				</view>
				<view class="register-input">
					<input :type="showRegisterPassword ? 'text' : 'password'" placeholder="密码"
						v-model="registerPassword" @input="checkPasswordFormat('register')" />
					<image v-if="registerPassword" class="password-eye"
						:src="showRegisterPassword ? '/static/images/eye-open.png' : '/static/images/eye-close.png'"
						@click="togglePasswordVisibility('register')" mode="aspectFit"></image>
				</view>
				<view v-if="registerPassword" class="register-password-tips">
					<view class="register-tip-item">
						<view class="register-tip-dot" :class="{ 'active': registerPasswordLengthValid }"></view>
						<text class="register-tip-text">至少6位</text>
					</view>
					<view class="register-tip-item">
						<view class="register-tip-dot" :class="{ 'active': registerPasswordComplexValid }"></view>
						<text class="register-tip-text">含有数字、字母、特殊字符中的任意两种</text>
					</view>
				</view>
				<view class="register-input">
					<input type="text" placeholder="昵称" v-model="nikeName" maxlength="12"
						@input="checkNicknameLength()" />
					<image class="refresh-icon" src="/static/images/refresh-icon.png" @click="generateRandomNickname()"
						mode="aspectFit"></image>
				</view>
				<view class="register-input register-code-box">
					<input style="width: 50%;" type="text" class="register-code" v-model="registerCode"
						placeholder="验证码" />
					<button @click="sendSignCode" :disabled="registerCodeCountdown < 60">
						{{ registerCodeCountdown < 60 ? `${registerCodeCountdown}秒后重发` : '发送验证码' }}
					</button>
				</view>


				<view class="gender-selector">
					<view class="gender-option" :class="{ 'male-active': gender === 0 }" @click="selectGender(0)">
						<image class="gender-icon" src="/static/images/male-icon.png" mode="aspectFit"></image>
						<text>男生</text>
					</view>
					<view class="gender-option" :class="{ 'female-active': gender === 1 }" @click="selectGender(1)">
						<image class="gender-icon" src="/static/images/female-icon.png" mode="aspectFit"></image>
						<text>女生</text>
					</view>
				</view>
			</view>
			<!-- 手机验证登录
			<!-- <view v-else-if="activeTab === 'quick'">
				<view class="input-group">
					<input type="number" class="input-field" placeholder="手机号" v-model="phone" maxlength="9" />
				</view>
				<view class="input-group code-group">
					<input type="number" class="input-field" placeholder="请输入验证码" v-model="code" maxlength="9" />
					<button class="code-btn" @click="getVerifyCode" :disabled="countdown < 60">
						{{ countdown < 60 ? `${countdown}s后重新获取` : '获取验证码' }} </button>
				</view>
			</view>




			<!-- 登录按钮 -->
			<button v-if="activeButton==='login'" class="login-btn" @click="handleLogin()">登录</button>
			<button v-else class="login-btn" @click="handleRegister()">注册</button>

			<!-- 用户协议 -->
			<view class="agreement">
				<view class="checkbox" :class="{ checked: isAgree }" @click="toggleAgreement">
					<view class="checkbox-inner" v-if="isAgree"></view>
				</view>
				<text class="agreement-text">登录即代表您同意</text>
				<text class="agreement-link" @click="goToUserAgreement">《用户协议》</text>
				<text class="agreement-link" @click="goToPrivacyPolicy">《隐私策略》</text>
			</view>

			<!-- 其他登录方式 -->
			<view class="login-other">
				<view class="login-other-title">
					<text>其他登录方式</text>
				</view>

				<view class="login-other-icon">
					<!-- 暂时关闭微信登录 -->
					<!-- 微信登录 -->
					<!-- <image class="login-other-icon-item" src="/static/images/wechatIcon.jpg" mode="scaleToFill"
						@click="wechatLogin" /> -->
				</view>
			</view>
		</view>

		<!-- 协议弹窗 -->
		<view class="agreement-popup" v-if="showAgreementPopup">
			<view class="agreement-popup-mask" @click="closeAgreementPopup"></view>
			<view class="agreement-popup-content">
				<view class="agreement-popup-title">请阅读并同意以下条款</view>
				<view class="agreement-popup-links">
					<text class="agreement-link" @click="goToUserAgreement">《用户协议》</text>
					<text class="agreement-link" @click="goToPrivacyPolicy">《隐私政策》</text>
					<!-- <text class="agreement-link" @click="goToChildPrivacy">《儿童隐私保护声明》</text>
					<text class="agreement-link" @click="goToAuthService">《联通统一认证服务条款》</text> -->
				</view>
				<view class="agreement-popup-btns">
					<view class="agreement-popup-btn agreement-popup-btn-confirm" @click="agreeAndContinue">同意并继续</view>
					<view class="agreement-popup-btn agreement-popup-btn-cancel" @click="closeAgreementPopup">不同意</view>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	import {
		passwordLogin,
		wxLogin,
		codeLogin,
		sendLoginCode
	} from '../../api/loginApi';
	import {
		getUserInfo
	} from '../../api/userApi';
	import {
		register,
		sendRegisterCode
	} from '../../api/registerApi';
	import globalStore from '../../store/global.js';
	import userInfo from '../../store/userInfo';
	import {
		updateMyViewTabBarText
	} from '../../utils/tabBarUtils.js';
	export default {
		data() {
			return {
				logincode: "",
				registerCode: "",
				loginway: 1, //1密码   0验证码
				logintext: "验证码登录",
				activeTab: 'password', // 默认选中密码登录
				activeButton: 'login', // 默认选中密码登录
				phone: '',
				code: '',
				username: '', //账号
				password: '',
				accountOrPhone: '', ///密码登录时的账号或手机号
				phone: '',
				registerPassword: '',
				registerPhone: '',
				nikeName: '',
				gender: null, //性别 0：男  1：女 2：未知
				isAgree: false, // 是否同意用户协议
				countdown: 60,
				showAgreementPopup: false, // 是否显示协议弹窗
				lastOperation: '', // 记录最后一次操作类型
				showPassword: false,
				showRegisterPassword: false,
				showRegisterPasswordEd: false,
				passwordLengthValid: false,
				passwordComplexValid: false,
				registerPasswordLengthValid: false,
				registerPasswordComplexValid: false,
				nicknameAdjectives: ['快乐', '阳光', '可爱', '酷酷', '萌萌', '帅气', '聪明', '活泼', '开心', '勇敢', '机智', '温柔', '善良', '幽默',
					'俏皮', '美丽'
				],
				nicknameNouns: ['小猫', '小狗', '小熊', '小兔', '小鹿', '小鸟', '小鱼', '小象', '小狐狸', '小猴子', '小老虎', '小企鹅', '小蜜蜂', '小松鼠',
					'小浣熊', '小海豚', '小海燕'
				],
				isNicknameMaxLength: false, // 标记昵称是否达到最大长度
				i: '',
				loginCodeCountdown: 60, // 登录验证码倒计时
				registerCodeCountdown: 60, // 注册验证码倒计时
				loginCodeTimer: null, // 登录验证码定时器
				registerCodeTimer: null // 注册验证码定时器
			}
		},
		methods: {
			// 添加返回方法
			goBack() {
				uni.navigateBack();
			},

			//初始话登录表单数据
			initializedLoginData() {
				this.phone = '';
				this.accountOrPhone = '';
				this.password = '';
			},
			//初始化注册表单数据
			initializedRegisterData() {
				this.registerPhone = '';
				this.registerPassword = '';
				this.registerPasswordEd = '';
				this.nikeName = '';
				this.gender = null;
			},


			// 微信登录
			wechatLogin() {
				if (!this.isAgree) {
					this.lastOperation = 'wechat';
					this.showAgreementPopup = true;
					return;
				}
				// 微信登录逻辑
				this.doWechatLogin();
			},

			// 执行微信登录
			doWechatLogin() {
				console.log('执行微信登录');
				// 这里添加微信登录的逻辑
				uni.login({
					provider: 'weixin',
					"onlyAuthorize": true, // 微信登录仅请求授权认证
					success: (res) => {
						if (res.code) {
							console.log('登录凭证:', res.code);
							// 将code发送到服务器，换取openid和session_key
							wxLogin(res.code).then(res => {
								if (res.code == 0) {
									console.log('登录成功:', res.data);
									uni.showToast({
										title: '登录成功',
										icon: 'none'
									});

									//保存token  关键字islogin
									let token = typeof res.data === 'string' ? res.data : (res.data
										.token || JSON.stringify(res.data));
									globalStore.setToken(token);
									globalStore.setIsLoginStype();

									console.log("全局token已设置:", globalStore.getToken());
									// 初始化注册表单数据
									this.initializedRegisterData();
									this.initializedLoginData();
									//获存储用户信息
									getUserInfo().then(res => {
										if (res.code == 0) {
											//随机选择一个头像
											if (res.data.avatarUrl == null) {
												// 随机选择一个头像
												const randomAvatars = [
													'../../static/images/noAvatar.jpg',
													'../../static/images/noAvatar01.jpg',
													'../../static/images/noAvatar02.jpg'
												];
												const randomIndex = Math.floor(Math.random() *
													3); // 生成0-2的随机数
												res.data.avatarUrl = randomAvatars[
													randomIndex];
											}
											//保存用户信息
											userInfo.setUserInfo(res.data);
										}
									});
									// 保存用户信息到本地存储

									//跳转到首页
									uni.switchTab({
										url: '/pages/home/home'
									});
								}
							})
						} else {
							console.error('登录失败:', res.errMsg);
						}
					},
					fail: (err) => {
						console.error('登录失败:', err);
					}
				});







				/* uni.showToast({
					title: '微信登录中...',
					icon: 'none'
				});

				// 模拟登录成功
				setTimeout(() => {
					uni.showToast({
						title: '登录成功',
						icon: 'success'
					});

					// 登录成功后跳转到首页
					setTimeout(() => {
						uni.switchTab({
							url: '/pages/home/home'
						});
					}, 1500);
				}, 1000); */
			},

			// 显示协议弹窗
			showAgreementDialog() {
				this.showAgreementPopup = true;
			},

			// 关闭协议弹窗
			closeAgreementPopup() {
				this.showAgreementPopup = false;
				this.lastOperation = ''; // 清空操作记录
			},

			// 同意协议并继续
			agreeAndContinue() {
				this.isAgree = true;
				this.showAgreementPopup = false;

				// 根据当前操作继续执行相应的登录逻辑
				switch (this.lastOperation) {
					case 'wechat':
						// 执行微信登录
						this.doWechatLogin();
						break;
					case 'login':
						// 执行普通登录
						this.doLogin();
						break;
					case 'verify':
						// 执行获取验证码
						this.doGetVerifyCode();
						break;
					default:
						// 默认不执行任何操作
						break;
				}

				// 清空操作记录
				this.lastOperation = '';
			},

			// 切换表单方式
			switchTab(tab) {
				this.activeTab = tab;
			},

			//切换按钮功能
			switchButton(button) {
				this.activeButton = button;
			},

			// 获取验证码
			/* getVerifyCode() {
				if (!this.isAgree) {
					this.lastOperation = 'verify';
					this.showAgreementPopup = true;
					return;
				}

				this.doGetVerifyCode();
			},
 */

			// 执行获取验证码
			/* doGetVerifyCode() {
				if (!this.phone || this.phone.length !== 11) {
					uni.showToast({
						title: '请输入正确的手机号',
						icon: 'none'
					});
					return;
				}

				// 这里添加获取验证码的逻辑
				uni.showToast({
					title: '验证码已发送',
					icon: 'none'
				});

				// 倒计时逻辑
				let timer = null;
				this.countdown = 60;
				timer = setInterval(() => {
					this.countdown--;
					if (this.countdown <= 0) {
						clearInterval(timer);
						this.countdown = 60;
					}
				}, 1000);
			}, */

			// 切换协议同意状态
			toggleAgreement() {
				this.isAgree = !this.isAgree;
			},

			// 处理登录
			handleLogin() {
				if (!this.isAgree) {
					this.lastOperation = 'login';
					this.showAgreementPopup = true;
					return;
				}
				this.doLogin();
			},

			// 处理注册
			handleRegister() {
				if (!this.isAgree) {
					this.lastOperation = 'register';
					this.showAgreementPopup = true;
					return;
				}
				//前往注册
				this.doRegister();
			},



			//执行注册
			doRegister() {
				//非空判断
				// 手机号不能为空
				if (!this.registerPhone) {
					uni.showToast({
						title: '请输入手机号',
						icon: 'none'
					});
					return;
				}
				//密码不能为空
				if (!this.registerPassword) {
					uni.showToast({
						title: '请输入密码',
						icon: 'none'
					});
					return;
				}
				//验证码不能为空
				if (!this.registerCode) {
					uni.showToast({
						title: '请输入验证码',
						icon: 'none'
					});
					return;
				}

				//昵称不能为空
				if (this.nikeName == null || this.nikeName == '') {
					uni.showToast({
						title: '请输入昵称',
						icon: 'none'
					});
					return;
				}
				//性别不能为空
				if (this.gender != 0 && this.gender != 1 && this.gender != 2) {
					uni.showToast({
						title: '请点击对应的性别图案',
						icon: 'none'
					});
					return;
				}

				//手机号检查
				//请输正确的手机号
				if (!/^\d{11}$/.test(this.registerPhone)) {
					uni.showToast({
						title: '请输入正确的手机号',
						icon: 'none'
					});
					return;
				}

				//密码检查
				//判断密码是否适合至少6位，且含有数字，字符，特殊字符中的任意俩种
				if (!
					/^(?=.*[0-9])(?=.*[a-zA-Z])(?=.*[^a-zA-Z0-9]).{6,}$|^(?=.*[0-9])(?=.*[a-zA-Z]).{6,}$|^(?=.*[0-9])(?=.*[^a-zA-Z0-9]).{6,}$|^(?=.*[a-zA-Z])(?=.*[^a-zA-Z0-9]).{6,}$/
					.test(this.registerPassword)) {
					uni.showToast({
						title: '密码格式错误',
						icon: 'none'
					});
					return;
				}


				//昵称检查
				//当昵称超过12位时显示亲,不能输入更多了~
				if (this.nikeName.length > 12) {
					uni.showToast({
						title: '昵称不能超过12个字符',
						icon: 'none'
					});
					//并且设置输入框为12个字符
					this.nikeName = this.nikeName.substring(0, 12);
					return;
				}


				//注册后台验证
				register({
					kyyUserRegisterDTO: {
						phone: this.registerPhone,
						password: this.registerPassword,
						nickName: this.nikeName,
						gender: this.gender
					},
					code: this.registerCode
				}).then(res => {
					if (res.code == 0) {
						uni.showToast({
							title: '注册成功',
							icon: 'none'
						});
						// 初始化注册表单数据
						this.initializedRegisterData();
						this.initializedLoginData();
						this.activeButton = 'login';
						this.activeTab = 'password';

						//回到登录tab表单
						// switchTab("password");
						//按钮切换
						// switchButton("login");
					} else {
						// 显示错误信息
						uni.showToast({
							title: res.msg,
							icon: 'none'
						});
					}
				})

			},


			// 执行密码登录逻辑
			doLogin() {
				if (this.loginway === 0) {
					// 手机号逻辑
					if (!this.accountOrPhone || this.accountOrPhone.length !== 11) {
						uni.showToast({
							title: '请输入正确的手机号',
							icon: 'none'
						});
						return;
					}

					if (!this.logincode) {
						uni.showToast({
							title: '请输入验证码',
							icon: 'none'
						});
						return;
					}

					codeLogin({
						phone:this.accountOrPhone,
						code:this.logincode
					}).then(res => {
						if (res.code == 0) {
							console.log("登录成功:", res.data);
							let token = typeof res.data === 'string' ? res.data : (res.data.token || JSON
								.stringify(res.data));
							// 使用全局变量存储
							globalStore.setToken(token);
							globalStore.setIsLoginStype();
							// console.log("全局token已设置:", globalStore.getToken());
							//改变tabbar的文字
							// uni.setTabBarItem({
							// 	index: 4,  // "我的"选项在tabBar中的索引，它是第5个选项（索引从0开始）
							// 	text: "我的"
							// });
							updateMyViewTabBarText();

							//获取用户个人信息
							getUserInfo().then(res => {
								// console.log("获取个人信息");
								if (res.code == 0) {
									if (res.data.avatarUrl == null) {
										// 随机选择一个头像
										const randomAvatars = [
											'../../static/images/noAvatar.jpg',
											'../../static/images/noAvatar01.jpg',
											'../../static/images/noAvatar02.jpg'
										];
										const randomIndex = Math.floor(Math.random() * 3); // 生成0-2的随机数
										res.data.avatarUrl = randomAvatars[randomIndex];
									}
									// console.log("存储个人信息",res.data);
									//保存用户信息
									userInfo.setUserInfo(res.data);
								}
							});
							// 初始化注册表单数据
							this.initializedRegisterData();
							this.initializedLoginData();
							uni.showToast({
								title: '登录成功',
								icon: 'none'
							});
							//跳转到首页
							uni.switchTab({
								url: '/pages/home/home'
							});

						}
					})
				} else if (this.loginway === 1) {
					//密码登录
					//非空判断
					if (this.accountOrPhone === '' || this.accountOrPhone == null) {
						uni.showToast({
							title: '请输入账号或手机号',
							icon: 'none'
						});
						return;
					}
					if (this.password === '' || this.password == null) {
						uni.showToast({
							title: '请输入密码',
							icon: 'none'
						});
						return;
					}
					//登录后台验证
					passwordLogin({
						accountOrPhone: this.accountOrPhone,
						password: this.password
					}).then(res => {
						if (res.code == 0) {
							console.log("登录成功:", res.data);
							let token = typeof res.data === 'string' ? res.data : (res.data.token || JSON
								.stringify(res.data));
							// 使用全局变量存储
							globalStore.setToken(token);
							globalStore.setIsLoginStype();
							// console.log("全局token已设置:", globalStore.getToken());
							//改变tabbar的文字
							// uni.setTabBarItem({
							// 	index: 4,  // "我的"选项在tabBar中的索引，它是第5个选项（索引从0开始）
							// 	text: "我的"
							// });
							updateMyViewTabBarText();

							//获取用户个人信息
							getUserInfo().then(res => {
								// console.log("获取个人信息");
								if (res.code == 0) {
									if (res.data.avatarUrl == null) {
										// 随机选择一个头像
										const randomAvatars = [
											'../../static/images/noAvatar.jpg',
											'../../static/images/noAvatar01.jpg',
											'../../static/images/noAvatar02.jpg'
										];
										const randomIndex = Math.floor(Math.random() * 3); // 生成0-2的随机数
										res.data.avatarUrl = randomAvatars[randomIndex];
									}
									// console.log("存储个人信息",res.data);
									//保存用户信息
									userInfo.setUserInfo(res.data);
								}
							});
							// 初始化注册表单数据
							this.initializedRegisterData();
							this.initializedLoginData();
							uni.showToast({
								title: '登录成功',
								icon: 'none'
							});
							//跳转到首页
							uni.switchTab({
								url: '/pages/home/home'
							});

						}
					})

				}

				/* // 模拟登录成功
				uni.showToast({
					title: '登录成功',
					icon: 'success'
				});

				// 登录成功后跳转到首页
				setTimeout(() => {
					uni.switchTab({
						url: '/pages/home/home'
					});
				}, 1500); */

			},



			// 跳转到用户协议
			goToUserAgreement() {
				uni.navigateTo({
					url: '/pages/yonghu/yonghu'
				});
			},

			// 跳转到隐私策略
			goToPrivacyPolicy() {
				uni.navigateTo({
					url: '/pages/yinsi/yinsi'
				});
			},

			// 跳转到儿童隐私保护声明
			goToChildPrivacy() {
				uni.navigateTo({
					url: '/pages/yinsi/yinsi'
				});
			},

			// 跳转到联通统一认证服务条款
			goToAuthService() {
				uni.navigateTo({
					url: '/pages/yinsi/yinsi'
				});
			},

			// 选择性别
			selectGender(gender) {
				this.gender = gender;
			},

			changeloginway() {
				if (this.loginway === 1) {
					this.loginway = 0;
					this.logintext = "密码登录";
				} else {
					this.loginway = 1;
					this.logintext = "验证码登录";
				}
			},
			// 忘记密码
			forgotPassword() {
				uni.showToast({
					title: '忘记密码功能开发中',
					icon: 'none'
				});
			},

			togglePasswordVisibility(type) {
				switch (type) {
					case 'password':
						this.showPassword = !this.showPassword;
						break;
					case 'register':
						this.showRegisterPassword = !this.showRegisterPassword;
						break;
					case 'registerEd':
						this.showRegisterPasswordEd = !this.showRegisterPasswordEd;
						break;
				}
			},

			// 检查密码格式
			checkPasswordFormat(type) {
				if (type === 'password') {
					// 检查长度是否至少6位
					this.passwordLengthValid = this.password.length >= 6;

					// 检查是否包含数字、字母、特殊字符中的任意两种
					let hasNumber = /\d/.test(this.password);
					let hasLetter = /[a-zA-Z]/.test(this.password);
					let hasSpecial = /[^a-zA-Z0-9]/.test(this.password);

					let typeCount = 0;
					if (hasNumber) typeCount++;
					if (hasLetter) typeCount++;
					if (hasSpecial) typeCount++;

					this.passwordComplexValid = typeCount >= 2;
				} else if (type === 'register') {
					// 检查长度是否至少6位
					this.registerPasswordLengthValid = this.registerPassword.length >= 6;

					// 检查是否包含数字、字母、特殊字符中的任意两种
					let hasNumber = /\d/.test(this.registerPassword);
					let hasLetter = /[a-zA-Z]/.test(this.registerPassword);
					let hasSpecial = /[^a-zA-Z0-9]/.test(this.registerPassword);

					let typeCount = 0;
					if (hasNumber) typeCount++;
					if (hasLetter) typeCount++;
					if (hasSpecial) typeCount++;

					this.registerPasswordComplexValid = typeCount >= 2;
				}
			},

			// 检查昵称长度
			checkNicknameLength() {
				if (this.nikeName.length >= 12) {
					this.isNicknameMaxLength = true;
					// 截断超过12个字符的部分
					this.nikeName = this.nikeName.substring(0, 12);
					// 显示提示
					uni.showToast({
						title: '不能输入更多了~',
						icon: 'none',
						duration: 1500
					});
				} else {
					this.isNicknameMaxLength = false;
				}
			},

			// 修改生成随机昵称方法，确保不超过12个字符
			generateRandomNickname() {
				const adjective = this.nicknameAdjectives[Math.floor(Math.random() * this.nicknameAdjectives.length)];
				const noun = this.nicknameNouns[Math.floor(Math.random() * this.nicknameNouns.length)];
				let nickname = adjective + noun;
				// 确保生成的昵称不超过12个字符
				if (nickname.length > 12) {
					nickname = nickname.substring(0, 12);
				}
				this.nikeName = nickname;
			},


			//获取用户个人信息
			doGetUserInfo() {
				getUserInfo().then(res => {
					if (res.code == 0) {
						console.log(res.data);
						//保存用户信息
						uni.setStorageSync('userInfo', res.data);
					}
				})
			},

			// 发送注册验证码
			sendSignCode() {
				if (!this.registerPhone) {
					uni.showToast({
						title: '请先输入手机号',
						icon: 'none'
					});
					return;
				}

				// 手机号格式验证
				if (!/^\d{11}$/.test(this.registerPhone)) {
					uni.showToast({
						title: '请输入正确的手机号',
						icon: 'none'
					});
					return;
				}
				
				// 如果倒计时不是60秒，说明正在倒计时中，不允许再次发送
				if (this.registerCodeCountdown < 60) {
					return;
				}

				uni.showToast({
					title: '验证码发送中...',
					icon: 'none'
				});

				// 这里添加实际发送验证码的API调用
				sendRegisterCode(this.registerPhone);

				// 开始倒计时
				this.registerCodeCountdown = 60;
				this.registerCodeTimer = setInterval(() => {
					this.registerCodeCountdown--;
					if (this.registerCodeCountdown <= 0) {
						clearInterval(this.registerCodeTimer);
						this.registerCodeCountdown = 60;
					}
				}, 1000);

				setTimeout(() => {
					uni.showToast({
						title: '验证码已发送',
						icon: 'success'
					});
				}, 1000);
			},
			// 发送登录验证码
			sendLoginCode() {
				if (!this.accountOrPhone) {
					uni.showToast({
						title: '请先输入手机号',
						icon: 'none'
					});
					return;
				}

				// 手机号格式验证
				if (!/^\d{11}$/.test(this.accountOrPhone)) {
					uni.showToast({
						title: '请输入正确的手机号',
						icon: 'none'
					});
					return;
				}
				
				// 如果倒计时不是60秒，说明正在倒计时中，不允许再次发送
				if (this.loginCodeCountdown < 60) {
					return;
				}

				uni.showToast({
					title: '验证码发送中...',
					icon: 'none'
				});

				// 这里添加实际发送验证码的API调用
				sendLoginCode(this.accountOrPhone);
				
				// 开始倒计时
				this.loginCodeCountdown = 60;
				this.loginCodeTimer = setInterval(() => {
					this.loginCodeCountdown--;
					if (this.loginCodeCountdown <= 0) {
						clearInterval(this.loginCodeTimer);
						this.loginCodeCountdown = 60;
					}
				}, 1000);

				setTimeout(() => {
					uni.showToast({
						title: '验证码已发送',
						icon: 'success'
					});
				}, 1000);
			},
		},
		// 添加页面加载时自动生成昵称
		mounted() {
			// 如果是注册页面，自动生成昵称
			if (this.activeTab === 'register') {
				this.generateRandomNickname();
			}
		},
		// 监听标签切换，当切换到注册页面时自动生成昵称
		watch: {
			activeTab(newVal) {
				if (newVal === 'register' && !this.nikeName) {
					this.generateRandomNickname();
				}
			}
		},
		// 组件销毁时清除定时器
		beforeDestroy() {
			// 清除登录验证码定时器
			if (this.loginCodeTimer) {
				clearInterval(this.loginCodeTimer);
			}
			// 清除注册验证码定时器
			if (this.registerCodeTimer) {
				clearInterval(this.registerCodeTimer);
			}
		}
	}
</script>

<style>
	/* 基础容器样式 */
	.login-container {
		padding: 30rpx;
		height: 100vh;
		background-color: #fff;
		display: flex;
		flex-direction: column;
		position: fixed;
		width: 100%;
		box-sizing: border-box;
		left: 0;
		top: 0;
		overflow-y: auto;
	}

	/* 顶部返回按钮 */
	.back-button {
		position: fixed;
		top: 40rpx;
		left: 30rpx;
		z-index: 999;
		padding: 15rpx;
	}

	.back-icon {
		width: 50rpx;
		height: 50rpx;
	}

	/* 头部区域样式 */
	.login-header {
		display: flex;
		flex-direction: column;
		align-items: center;
		margin-top: 100rpx;
		margin-bottom: 60rpx;
	}

	.login-logo-container {
		width: 150rpx;
		height: 150rpx;
		margin-bottom: 30rpx;
		border-radius: 30rpx;
		display: flex;
		justify-content: center;
		align-items: center;
		padding: 10rpx;
	}

	.login-logo {
		width: 100%;
		height: 100%;
		border-radius: 20rpx;
	}

	.login-welcome {
		font-size: 48rpx;
		font-weight: bold;
		color: #333;
		margin-bottom: 10rpx;
	}

	.login-subtitle {
		font-size: 32rpx;
		color: #666;
	}

	/* 标签页切换 */
	.login-tabs {
		display: flex;
		justify-content: center;
		margin-bottom: 50rpx;
	}

	.tab-item {
		position: relative;
		padding: 0 30rpx;
		margin: 0 20rpx;
		font-size: 32rpx;
		color: #999;
		height: 60rpx;
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;
	}

	.tab-item text {
		position: relative;
		z-index: 1;
	}

	.tab-item.active {
		color: #FF6A00;
		font-weight: bold;
	}

	.tab-line {
		position: absolute;
		bottom: 0;
		left: 0;
		width: 100%;
		height: 4rpx;
		background-color: #FF6A00;
		transition: all 0.5s ease;
	}

	/* 表单容器 */
	.login-form {
		flex: 1;
		display: flex;
		flex-direction: column;
		overflow-y: auto;
		padding-bottom: 40rpx;
		width: 100%;
		box-sizing: border-box;
	}

	.login-form-box {
		display: flex;
		justify-content: center;
		flex-direction: column;
		/* align-items: flex-start; */
	}

	.login-password {
		margin-top: 20px;
		display: flex;
		justify-content: center;
		flex-direction: column;
		align-items: center;
	}

	.inputf {
		width: 80%;
		position: relative;
		margin-bottom: 20px;
		/* transition: 1s ease-in-out; */
		/* margin: auto; */
		display: flex;
		justify-content: center;
		flex-direction: column;
		align-items: flex-start;
	}

	.inputf input {
		width: 100%;
		height: 35px;
		border: none;
		border-radius: 18px;
		border: 1.5px solid rgb(255, 106, 0);
		/* outline: 1.5px solid rgb(255, 106, 0); */
		background: transparent;
		font-size: 12px;
		color: rgb(0, 0, 0);
		padding-left: 10px;
	}

	.inputf input::placeholder {
		color: rgb(255, 106, 0);
	}

	.inputf input:focus {
		outline: 1.5px solid rgb(255, 106, 0);
	}

	/* .inputf input:focus::placeholder {
		opacity: 0 ;
	} */

	/* .inputf input:focus +.label {
		opacity: 1 ;
		top: -20px;
	} */

	/* .inputf input:not(:placeholder-shown)+.label {
		top: -20px;
		border: #fff 1px solid;
	} */
	.inputf-box {
		width: 80%;
		position: relative;
		display: flex;
	}

	.label {
		position: absolute;
		top: 10px;
		left: 23rpx;
		color: rgb(255, 106, 0);
		font-size: 12px;
		font-weight: bold;
		transition: 0.25s ease-out;
		opacity: 1;
	}

	.login-sendcode {
		width: 40%;
		height: 38px;
		border-radius: 0px 18px 18px 0px;
		background-color: rgb(17, 52, 255);
		color: #ffffff;
		display: flex;
		justify-content: center;
		align-items: center;
		position: relative;
		right: calc(-4%);
	}
	
	.login-sendcode.disabled {
		background-color: #a0a0a0;
		color: #e0e0e0;
	}




	/* 通用按钮样式 */
	.login-btn {
		width: 88%;
		height: 100rpx;
		line-height: 100rpx;
		background-color: #FF6A00;
		color: #fff;
		font-size: 32rpx;
		border-radius: 50rpx;
		display: block;
	}

	/* 协议勾选区域 */
	.agreement {
		display: flex;
		align-items: center;
		font-size: 24rpx;
		width: 80%;
		margin: 5px 0px 0px 12%;
		/* background-color: #2943ef; */
	}

	.checkbox {
		width: 30rpx;
		height: 30rpx;
		border-radius: 50%;
		border: 1px solid #ccc;
		margin-right: 10rpx;
		display: flex;
		justify-content: center;
		align-items: center;
	}

	.checkbox.checked {
		border-color: #FF6A00;
		background-color: #FF6A00;
	}

	.checkbox-inner {
		width: 16rpx;
		height: 16rpx;
		border-radius: 50%;
		background-color: #fff;
	}

	.agreement-text {
		color: #999;
	}

	.agreement-link {
		color: #FF6A00;
		margin-left: 5rpx;
	}

	/* 其他登录方式 */
	.login-other {
		position: absolute;
		bottom: 10%;
		display: flex;
		flex-direction: column;
		align-items: center;
		margin-top: 60rpx;
		width: 88%;
		margin-left: auto;
		margin-right: auto;
	}

	.login-other-title {
		position: relative;
		width: 100%;
		text-align: center;
		margin-bottom: 40rpx;
	}

	.login-other-title text {
		position: relative;
		display: inline-block;
		padding: 0 30rpx;
		font-size: 28rpx;
		color: #999;
		background-color: #fff;
	}

	.login-other-title::before,
	.login-other-title::after {
		content: '';
		position: absolute;
		top: 50%;
		width: 30%;
		height: 1px;
		background-color: #eee;
	}

	.login-other-title::before {
		left: 0;
	}

	.login-other-title::after {
		right: 0;
	}

	.login-other-icon {
		display: flex;
		justify-content: center;
		align-items: center;
		width: 100%;
	}

	.login-other-icon-item {
		width: 87rpx;
		height: 87rpx;
		border-radius: 50%;
		margin: 0 30rpx;
		box-shadow: 0 4rpx 12rpx rgba(0, 0, 0, 0.1);
		/* transition: all 0.3s ease; */
	}

	.login-other-icon-item:active {
		transform: scale(0.95);
		box-shadow: 0 2rpx 6rpx rgba(0, 0, 0, 0.1);
	}

	/* 密码登录表单样式 */
	.input-group {
		position: relative;
		margin-bottom: 30rpx;
		width: 100%;
		display: flex;
		justify-content: center;
	}

	.input-field {
		width: 88%;
		height: 100rpx;
		background-color: #F5F5F5;
		border-radius: 50rpx;
		padding: 0 100rpx 0 40rpx;
		font-size: 32rpx;
		color: #333;
		box-sizing: border-box;
	}

	.password-eye {
		position: absolute;
		right: 20rpx;
		top: 50%;
		transform: translateY(-50%);
		width: 44rpx;
		height: 44rpx;
		z-index: 2;
		padding: 20rpx;
	}

	.login-options {
		display: flex;
		justify-content: space-between;
		width: 80%;
		margin: auto;
	}

	.login-option {
		font-size: 28rpx;
		color: #FF6A00;
		padding: 10rpx;
	}

	/* 验证码相关样式 */
	.code-group {
		display: flex;
		align-items: center;
	}

	.code-group .input-field {
		flex: 1;
	}

	.code-btn {
		position: absolute;
		right: 10rpx;
		height: 70rpx;
		line-height: 70rpx;
		padding: 0 30rpx;
		background-color: #FF6A00;
		color: #fff;
		font-size: 24rpx;
		border-radius: 35rpx;
	}

	.code-btn[disabled] {
		background-color: #FFB380;
		color: rgba(255, 255, 255, 0.8);
	}

	/* 注册表单专用样式 */
	.register-form {
		display: flex;
		flex-direction: column;
		align-items: center;
		width: 100%;
		padding-bottom: 20rpx;
	}

	.register-input {
		position: relative;
		margin-bottom: 25rpx;
		width: 80%;
		border-radius: 20px;
		/* background-color: #4CAF50; */
		border: 1px solid #FF6A00;
	}

	.register-input input {
		width: 100%;
		height: 35px;
		border-radius: 10px;
		margin-left: 20px;

	}

	.register-code-box {
		display: flex;
	}

	.register-code-box button {
		width: 50%;
		border-radius: 0px 20px 20px 0px;
		height: 35px;
		font-size: 14px;
		background-color: #2943ef;
		color: #ffffff;
	}




	.register-input-group {
		position: relative;
		margin-bottom: 25rpx;
		width: 88%;
	}

	.refresh-icon {
		position: absolute;
		right: 40rpx;
		top: 50%;
		transform: translateY(-50%);
		width: 44rpx;
		height: 44rpx;
		z-index: 2;
		padding: 20rpx;
		/* transition: all 0.3s ease; */
	}

	.refresh-icon:active {
		transform: translateY(-50%) rotate(180deg);
	}

	/* 密码提示样式 */
	.register-password-tips {
		width: 80%;
		margin: 0 auto 20rpx;
		background-color: #FFFBF5;
		border-radius: 16rpx;
		padding: 15rpx 25rpx;
		box-shadow: 0 2rpx 8rpx rgba(255, 106, 0, 0.1);
	}

	.register-tip-item {
		display: flex;
		align-items: center;
		margin-bottom: 12rpx;
	}

	.register-tip-dot {
		width: 16rpx;
		height: 16rpx;
		border-radius: 50%;
		background-color: #ccc;
		margin-right: 15rpx;
		/* transition: all 0.3s ease; */
	}

	.register-tip-dot.active {
		background-color: #FF6A00;
		box-shadow: 0 0 5rpx rgba(255, 106, 0, 0.5);
		/* transition: all 0.3s ease; */
	}

	.register-tip-text {
		font-size: 26rpx;
		color: #666;
	}

	/* 注册验证码样式 */
	.register-code-group {
		display: flex;
		justify-content: space-between;
		width: 88%;
		margin-bottom: 25rpx;
	}

	.register-code-input {
		width: 62%;
		height: 100rpx;
		background-color: #F5F5F5;
		border-radius: 50rpx;
		padding: 0 30rpx;
		font-size: 32rpx;
		color: #333;
		box-sizing: border-box;
	}

	.register-code-btn {
		width: 35%;
		height: 100rpx;
		line-height: 100rpx;
		background-color: #FF6A00;
		color: #fff;
		font-size: 28rpx;
		border-radius: 50rpx;
		text-align: center;
	}

	/* 性别选择器样式 */
	.gender-selector {
		display: flex;
		justify-content: center;
		width: 88%;
		/* margin: 10rpx auto 30rpx; */
	}

	.gender-option {
		display: flex;
		align-items: center;
		justify-content: center;
		padding: 20rpx 0;
		border-radius: 50rpx;
		background-color: #F5F5F5;
		/* transition: all 0.3s ease; */
		margin: 0 15rpx;
		flex: 1;
	}

	.gender-option text {
		margin-left: 10rpx;
		font-size: 28rpx;
		color: #333;
	}

	.gender-icon {
		width: 48rpx;
		height: 48rpx;
	}

	.male-active {
		background-color: rgba(0, 122, 255, 0.15);
		box-shadow: 0 4rpx 15rpx rgba(0, 122, 255, 0.2);
	}

	.female-active {
		background-color: rgba(255, 45, 85, 0.15);
		box-shadow: 0 4rpx 15rpx rgba(255, 45, 85, 0.2);
	}

	/* 协议弹窗样式 */
	.agreement-popup {
		position: fixed;
		top: 0;
		left: 0;
		right: 0;
		bottom: 0;
		z-index: 999;
	}

	.agreement-popup-mask {
		position: absolute;
		top: 0;
		left: 0;
		right: 0;
		bottom: 0;
		background-color: rgba(0, 0, 0, 0.5);
	}

	.agreement-popup-content {
		position: absolute;
		left: 50%;
		top: 50%;
		transform: translate(-50%, -50%);
		width: 80%;
		background-color: #fff;
		border-radius: 20rpx;
		padding: 40rpx;
		display: flex;
		flex-direction: column;
		align-items: center;
	}

	.agreement-popup-title {
		font-size: 36rpx;
		font-weight: bold;
		color: #333;
		margin-bottom: 30rpx;
		text-align: center;
	}

	.agreement-popup-links {
		display: flex;
		flex-direction: column;
		align-items: center;
		margin-bottom: 40rpx;
	}

	.agreement-popup-links .agreement-link {
		margin: 10rpx 0;
		color: #4CAF50;
		font-size: 28rpx;
	}

	.agreement-popup-btns {
		display: flex;
		flex-direction: column;
		width: 100%;
		align-items: center;
	}

	.agreement-popup-btn {
		width: 80%;
		height: 80rpx;
		line-height: 80rpx;
		text-align: center;
		border-radius: 40rpx;
		font-size: 30rpx;
		margin: 10rpx 0;
	}

	.agreement-popup-btn-cancel {
		background-color: #f5f5f5;
		color: #999;
	}

	.agreement-popup-btn-confirm {
		background-color: #4CAF50;
		color: #fff;
	}
</style>